package com.rljk.controller.app;/**
 * Created by mac on 2018/7/13.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.CouponUserManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CouponUserInController
 * @Description
 * @Author zhang
 * @Date 2018/7/13 14:32
 */
@RestController
@RequestMapping("couponuser")
@Slf4j
public class CouponUserInController extends BaseController {

    @Resource(name = "couponuserService")
    private CouponUserManager couponuserService;

    @Autowired
    RedisService redisService;

    /**
     * @Author zhang
     * @Description  用户优惠券列表
     * @Date 2018/7/13 14:37
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping("/usercplist")
    public Object couponUserListByUserId() {
        log.info("用户优惠券列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("COUPONUSERLISTBYUSERID", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("couponUserListByUserId",pd)){

                    //1.获取用户信息
                    String token = redisService.get(pd.getString("TOKEN"));
//                    System.out.println(token);
                    if(Tools.isEmpty(token)){
                        return Result.error(CodeMsg.TOKEN_ERROR);
                    }
                    pd.put("USER_ID",token);
                    List<PageData> couponList = couponuserService.listByUserId(pd);

                    return Result.success(couponList);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
